/* 
* @author           狄兆璐 
* @date             2018 06 30

* 从本地相册选择图片或使用相机拍照
*/
export let img = () => {
  return new Promise((resolve, reject) => {
    wx.chooseImage({
      count: 10,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) { resolve(res) },
      fail: function (err) {
      }
    })
  })
}
/* 
* @author           狄兆璐 
* @date             2018 06 30
* 拍摄视频或从手机相册中选视频，返回视频的临时文件路径
*/
export let video = () => {
  return new Promise((resolve, reject) => {
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      compressed: true,
      camera: 'back',
      success: function (res) { resolve(res) },
      fail: function (err) {
      }
    })
  })
}
/* 
* @author           狄兆璐 
* @date             2018 06 30
* 上传音视频到服务器
*/
export let uploadFile = (url, path) => {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: url,
      filePath: path,
      name: 'file',
      formData: {
        name: "houseHiddenCall/${filename}",
        key: "houseHiddenCall/${filename}",
        policy: "eyJleHBpcmF0aW9uIjoiMjAyMC0wMS0wMVQxMjowMDowMC4wMDBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF1dfQ==",
        OSSAccessKeyId: "LTAIuBMF9MOPd8um",
        signature: "1DMI2QG7/LrT0ppEnVSRPHeB++E=",
        success_action_status: "200"
      },
      success: function (res) {
        resolve(res)
      },
      fail: function () {
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        })
      }
    })
  })
}
/* 
* @author           狄兆璐 
* @date             2018 07 01
*  拨打电话
*/
export let makePhoneCall = () => {
  const app = getApp();
  return new Promise((resolve, reject) => {
    wx.makePhoneCall({
      phoneNumber: app.phonenumber,
      success: function (res) {
        resolve(res)
      },
      fail: function () {
        reject('err')
      }
    })
  })
}
/* 
* @author           狄兆璐 
* @date             2018 07 08
* 传code
*/
export let getToken = (data) => {
  return new Promise((resolve, reject) => {
    wx.request({
      // url: 'https://gateway.allhome.com.cn/auth/authentication/code',
      url: 'https://dgateway.allhome.com.cn/hiddencallmini/platformHouseHiddenCall/miniapp/logIn/isLogIn',
      data: data,
      method: 'post',
      // header: { 'content-type': 'application/x-www-form-urlencoded' },
      success(res) {
        res.statusCode === 200 ? resolve(res.data) : reject(res.data);
      },
      fail() {
        wx.showToast({
          title: '请求错误',
          icon: 'none'
        })
      }
    })
  })
}

/* 
* @author           狄兆璐 
* @date             2018 07 07
*  登录
*/
export let loginHttp = (data, uuid) => {
  return new Promise((resolve, reject) => {
    wx.request({
      // url: `http://192.168.10.200:8889/base/authentication/account/accountPassword`,
      url: `https://dgateway.allhome.com.cn/hiddencallmini/platformHouseHiddenCall/miniapp/logIn`,
      data,
      method: 'post',
      header: { 'openId': uuid },
      success(res) {
        res.statusCode === 200 ? resolve(res.data) : reject(res.data);
      },
      fail() {
        wx.showToast({
          title: '请求错误',
          icon: 'none'
        })
      }
    })
  })
}

export let updatePassword = (data, uuid) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `https://dgateway.allhome.com.cn/hiddencallmini/platformHouseHiddenCall/miniapp/forgetPassword/auth`,
      data,
      method: 'post',
      header: { 'openId': uuid },
      success(res) {
        res.statusCode === 200 ? resolve(res.data) : reject(res.data);
      },
      fail() {
        wx.showToast({
          title: '请求错误',
          icon: 'none'
        })
      }
    })
  })
}

/* 
* @author          wl 
* @date             2018 12 10
*  登录
*/
export let wxloginHttp = (data) => {
  return new Promise((resolve, reject) => {
    wx.request({
      // url: `https://gateway.allhome.com.cn/auth/authentication/mobile`,
      url: `https://dgateway.allhome.com.cn/hiddencallmini/platformHouseHiddenCall/miniapp/logIn/WxLogin`,
      data,
      method: 'post',
      // header: { 'content-type': 'application/x-www-form-urlencoded', 'deviceId': uuid, "x-regist-method": 'socialMobile' },
      // header: { 'deviceId': uuid },
      success(res) {
        res.statusCode === 200 ? resolve(res.data) : reject(res.data);
      },
      fail() {
        wx.showToast({
          title: '请求错误',
          icon: 'none'
        })
      }
    })
  })
}

/* 
* @author           狄兆璐 
* @date             2018 07 18
*  退出登录
*/
export let loginExit = (data) => {
  return new Promise((resolve, reject) => {
    const app = getApp();
    wx.request({
      url: `https://dgateway.allhome.com.cn/hiddencallmini/platformHouseHiddenCall/miniapp/logIn/deleteLogIn`,
      method: 'get',
      header: { 'content-type': 'application/json', 'Accept': 'application/json', "token": app.globalData.Authorization },
      success(res) {
        res.statusCode === 200 ? resolve(res.data) : reject(res.data);
      },
      fail() {
        wx.showToast({
          title: '请求错误',
          icon: 'none'
        })
      }
    })
  })
}

/* 
* @author           王磊 
* @date             2019-3-28
* 获取登录wx.code
*/
export let loginCode = (data) => {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout: 60000,
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        res.code ? resolve(res) : reject(res.errMsg)
        // if (res.statusCode === 200) {
        //   getToken({"wxCode": res.code}).then(res => {
        //     // 获取相关认证属性
        //     if (res.result) {
        //       app.globalData.uuid = res.result.openId
        //       app.globalData.Authorization = res.result.token
        //     }
        //     // 有绑定数据
        //     if (res.statusCode === 200) {
        //       this.callTask(false)
        //     } else {
        //       //无绑定数据
        //     }
        //   })
        // }
      },
      fail: function () {
        // 错误提示
        wx.showToast({ title: '请检查网络', icon: 'none', duration: 1500 })
      }
    })
  })
}

/* 
* @author           王磊
* @date             2019-3-28
* 企业微信登录
*/
export let qyLogin = (data) => {
  if (!wx.qy) return
  return new Promise((resolve, reject) => {
    wx.qy.login({
      timeout: 60000,
      success(data) {
        let { code } = data
        let corpsecret = `81SarFObN850yfRN6xFjOyXa00MOCr1xb56hI7qiELc`
        // wx.request({
        //   url: `http://192.168.10.58:8899/base/authentication/account/loginCheck/${code}/${corpsecret}`,
        //   method: "get",
        //   success(res) {
        //     resolve(res)
        //   },
        //   fail(error) {
        //     // 错误提示
        //     wx.showToast({ title: '请检查网络', icon: 'none', duration: 1500 })
        //     reject(error)
        //   }
        // })
      }
    })
  })
}